package Test;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author 徐志荣
 * @date 2023/3/6 9:50
 */
public class StudentSelect {
    public static void main(String[] args) throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/test4?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("123456");

        Connection connection = dataSource.getConnection();

        String sql = "select * from student";
        PreparedStatement statement = connection.prepareStatement(sql);

       ResultSet resultSet =  statement.executeQuery();

        while(resultSet.next()){
          int id =   resultSet.getInt("stuId");
          String name = resultSet.getString("stuName");
            System.out.println("id = "+id + ",name = " + name);
        }
        resultSet.close();
        statement.close();
        connection.close();
    }
}
